package com.suitsoft.ut.web.dabian;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.base.Team;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.CollegeManager;
import com.suitsoft.ut.service.base.GroupManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.util.StringUtils;
import com.suitsoft.ut.web.CrudActionSupport;

/**
 * 确定答辩时间及地点
 *@author Suineg
 */
@SuppressWarnings("serial")
@Results({@Result(name=CrudActionSupport.RELOAD,location="confirm-dabian-time",type="redirect")})
public class ConfirmDabianTimeAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	//Action's Result
	private final String RELOAD = "reload";
	
	
	/** 学院管理 服务层对象 */
	public CollegeManager collegeManager;
	/** 团队管理  */
	private GroupManager groupManager;
	/** 流程管理  */
	private ThesisFileManager fileManager;
	
	
	/** 团队集合 */
	private List<Team> groups = Lists.newArrayList();
	/** 页面值 的集 */
	private List<Team> valueList = Lists.newArrayList();
	/** 可用学院 */
	private List<College> colleges;
	
	@Override
	public String execute() throws Exception {
		//获取所用学院及答辩团队
		colleges = collegeManager.getByUser();
		for(College c : colleges){
			List<Team> teams = groupManager.findAllByCollege(c.getId().toString());
			for(Team t : teams){
				if("3".equals(t.getType())){
					groups.add(t);
				}
			}
		}
		return SUCCESS;
	}
	
	@LogAnnotation(log = "确定答辩时间及地点 - 保存")
	public String save(){
		if(valueList == null || valueList.isEmpty()){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		
		
		try {
			ThesisFile thesisFile = null;
			for(Team t : valueList){
				Team team = groupManager.get(t.getId());
				if(!StringUtils.isEmpty(t.getDabianAddress())){
					team.setDabianAddress(t.getDabianAddress());
				}
				if(t.getDabianDate() != null){
					team.setDabianDate(t.getDabianDate());
				}
				groupManager.save(team);
				
				//为此团队下的论文设置为 已确定答辩地址
				for(Student s : team.getStudents()){
					thesisFile = s.getThesis().getThesisFile();
					thesisFile.setIsConfirmDabianAddress(1L);
					fileManager.save(thesisFile);
				}
			}
			addActionMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
			return RELOAD;
		}
		return RELOAD;
	}

	public List<Team> getGroups() {
		return groups;
	}
	public void setGroups(List<Team> groups) {
		this.groups = groups;
	}
	public List<Team> getValueList() {
		return valueList;
	}
	public void setValueList(List<Team> valueList) {
		this.valueList = valueList;
	}
	public List<College> getColleges() {
		return colleges;
	}
	public void setColleges(List<College> colleges) {
		this.colleges = colleges;
	}
	@Autowired
	public void setCollegeManager(CollegeManager collegeManager) {
		this.collegeManager = collegeManager;
	}
	@Autowired
	public void setGroupManager(GroupManager groupManager) {
		this.groupManager = groupManager;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
}

